package com.xwn.provider.dubbo;

import com.alibaba.dubbo.config.annotation.Service;
import com.xwn.api.service.IHelloApi;
import com.xwn.bean.constant.RedisNameConstant;
import com.xwn.provider.constant.RedisConstants;
import com.xwn.provider.mapper.IHelloMapper;
import com.xwn.provider.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 测试Service
 * @Author duandw
 * @Date 2019/10/24 16:11
 **/
@Slf4j
@org.springframework.stereotype.Service
@Service(interfaceClass = IHelloApi.class)
public class HelloApiImpl implements IHelloApi {
//    @Autowired
//    private JedisUtil jedis;

    @Autowired
    private RedisUtil redis;

    @Autowired
    private IHelloMapper helloMapper;

    /**
     * 查詢say的話  默認查詢緩存
     * @param  userName 用户名称
     * @return
     */
    @Override
    public String sayHello(String userName) {
        String key = new StringBuilder(RedisNameConstant.CESHI_FILE_NAME).append(userName).toString();
        if (redis.hasKey(key)) {
            log.info("查詢 "+RedisNameConstant.CESHI_FILE_NAME+" 緩存");
            return (String)redis.get(key);
        }
        String str = helloMapper.sayHelloByName(userName);
        redis.set(key,str, RedisConstants.EXPRIRE_30_M_TIME);
        return str;
    }
}
